package zm.irc.threads;

import org.apache.log4j.Logger;
import zm.irc.client.IrcChannel;
import zm.irc.client.IrcClient;
import zm.irc.ioc.annotation.AutoWired;
import zm.irc.ioc.annotation.Service;
import zm.irc.message.processor.MessageProcessorCenter;
import zm.irc.message.receive.IrcReceiveMessage;

@Service
public class RecvMsgProcessThread extends IrcThread{
    private static Logger log = Logger.getLogger(RecvMsgProcessThread.class);

    @AutoWired
    private IrcClient ircClient;

    @AutoWired
    private MessageProcessorCenter msgProcessorCenter;



    @Override
    public void beforeLoop(){
        log.info("Recv Thread Start.");
    }


    @Override
    public void doRun() {
        IrcReceiveMessage receivedMsg;
        IrcReceiveMessage systemMsg;
        IrcChannel currentChannel;
        try {
            currentChannel  = this.ircClient.getCurrentChannel();
            if (currentChannel == null) {
                Thread.sleep(50);
                return;
            }
            receivedMsg = currentChannel.receiveMsg();
            systemMsg = this.ircClient.getSystemMsg();
            if (receivedMsg == null && systemMsg == null) {
                Thread.sleep(50);
                return;
            }
            this.msgProcessorCenter.process(systemMsg);
            this.msgProcessorCenter.process(receivedMsg);
        } catch (Exception e) {
            log.error("error", e);
        }
    }
}
